/*
 * NormalFitness.java
 *
 * Created on den 30 mars 2007, 23:30
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package grex.fitnessfunctions.Classification;


import grex.Data.Prediction;
import grex.Nodes.TargetPred;
import grex.fitnessfunctions.FitnessFunction;
import grex.fitnessfunctions.IFitnessFunction;

import java.io.Serializable;


/**
 *
 * @author rik
 */
public class REX_Fitness extends FitnessFunction implements IFitnessFunction, Serializable{
    /** Creates a new instance of NormalFitness */
    public REX_Fitness() {
        super("REX");
    }
    
    @Override
    protected double calcPredictionError(Prediction prediction, double targetValue) {
        if(prediction.getLeaf() instanceof TargetPred)
            return prediction.getPrediction() == targetValue ? 0 : 10;
        else
            return 1;
    }
    
    /*public double calcFitness(GP gp) throws GeneException {
        
   /*     Double chachedFitness = memmory.get(gp.getHashString());
        if(chachedFitness!=null){
            hashSaves ++;
            return chachedFitness;
        }
        if(memmory.size()%100==0)
            System.out.println(memmory.size() + " Save:"+hashSaves + " ratio:" +hashSaves/(1.0 + memmory.size()));

        gp.train();
        Options options = gp.getOptions();
        
      
        
        
        PredictionContainer  pcVal = gp.getPcVal();
        if(pcVal.values().size()>0)
            gp.execute(pcVal);                    
        double trainError=0,valFitness=0;
        
        for(Prediction p:gp.getPcTrain().values()){
            if(p.getPrediction()!=p.getTargetValue()){
                trainError++;
            }
        
        }
        for(Prediction p:gp.getPcVal().values())
            if(p.getPrediction()!=p.getTargetValue())
                valFitness++;

        trainError=trainError/gp.getPcTrain().size();

        trainError = 100*trainError;
                
        if(gp.getLength()>options.getMAX_TREE_SIZE())
            trainError += gp.getLength();
        trainError += gp.getLength()*options.getPUNISHMENT_FOR_LENGTH();
      //  memmory.put(gp.getHashString(), trainError);
        return trainError;

    }//*/


    
    
}
